package com.springboot.java.design.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: TongRui乀
 * @Date: 2020/7/5 10:04
 * @description：
 */
@Slf4j
public class TestCyclicBarrier {

    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
            log.info("人数凑齐了开整。。。。。。");
        });

        ExecutorService executors = Executors.newFixedThreadPool(9);

        for (int i = 0; i < 9; i++) {

            executors.execute(()->{
                String threadName = Thread.currentThread().getName();

                log.info("{}-正在来的路上", threadName);

                try {
                    Thread.sleep(new Random().nextInt(10000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                log.info("{}-到了", threadName);

                try {
                    cyclicBarrier.await();
                }catch (Exception e) {
                    e.printStackTrace();
                }

            });

        }

        executors.shutdown();

    }

}
